package org.amphiprion.rfreplayanalyser.ui.race;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

@SuppressWarnings("serial")
public class DriverTableCellRenderer extends DefaultTableCellRenderer {
	private RaceProvider raceProvider;
	private JCheckBox chk = new JCheckBox();

	public DriverTableCellRenderer(RaceProvider raceProvider) {
		this.raceProvider = raceProvider;
	}

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

		DriverItem item = ((DriverListTableModel) table.getModel()).getDriverAtPos(row);
		isSelected = raceProvider.isDriverItemSelected(item);
		Component cmp = null;
		if (column == 0) {
			cmp = chk;
			chk.setSelected(item.shown);
		} else {
			cmp = this;
			super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
			if (column == 1) {
				super.setBackground(item.color);
				if (item.color.getRed() <= 170 && item.color.getGreen() <= 170 && item.color.getBlue() <= 170) {
					super.setForeground(Color.WHITE);
				} else {
					super.setForeground(Color.BLACK);
				}
			} else {
				if (!isSelected) {
					cmp.setBackground(table.getBackground());
					cmp.setForeground(table.getForeground());
				}
				if (item.running) {
					cmp.setForeground(Color.BLACK);
				} else {
					cmp.setForeground(Color.LIGHT_GRAY);
				}
			}
		}

		return cmp;
	}
}
